package com.ch.sys.service;

import java.sql.Timestamp;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ch.fastbuilder.common.PageResult;
import com.ch.sys.model.Role;
import com.ch.sys.utils.ParamMap;
import com.ch.sys.utils.Response;

@Service
public class RoleService  {

	//@Transaction(readOnly = true, DataSource = "slaveDataSource")
	@Transactional
	public Response findPage(ParamMap param) {
		Response response = Response.newResponse();
		
		PageResult pageResult = Role.findByPage(param);
		return response.setPageResults(pageResult);
	}
	
	@Transactional
	public Role getByEmployeeId(Long employeeId) {
		Role role = Role.getByEmployeeId(employeeId);
		return role;
	}
	
	//@Transaction(readOnly = false, DataSource = "masterDataSource")
	@Transactional
	public Response add(Role role) {
		Response response = Response.newResponse();
		
		Timestamp time = new Timestamp(System.currentTimeMillis());
		role.set(Role.CREATED, time);
		role.create();
		
		//事务测试
		role.set(Role.NAME, "=====" + role.getString(Role.NAME) + "=====");
		this.update(role);
		
		return response.OK();
	}
	
	@Transactional
	public Response update(Role role) {
		Response response = Response.newResponse();
		role.update();
		
		if(role != null) {
			throw new RuntimeException("测试事务异常回滚");
		}
		
		return response.OK();
	}
}
